package com.mybatispuls.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatispuls.springboot.Utils.R;
import com.mybatispuls.springboot.entity.UcenterMember;
import com.mybatispuls.springboot.entity.Vo.LoginVo;
import com.mybatispuls.springboot.entity.Vo.UcenterVo;
import com.mybatispuls.springboot.entity.Vo.UserVo;
import com.mybatispuls.springboot.service.impl.UcenterMemberServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-22
 */
@RestController
@RequestMapping("/springboot/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberServiceImpl ucenterMemberService;

    //登录
    @PostMapping("loginInfo")
    public R loginInfo(UcenterMember ucenterMember){
        String login = ucenterMemberService.login(ucenterMember);
        return R.ok();
    }
    //注册
    @PostMapping("registerUser")
    public R registerUser(UcenterVo ucenterVo){
        ucenterMemberService.insert(ucenterVo);
        return R.ok();
    }

    //用户分页查询
    @PostMapping("UserPage/{current}/{limit}")
    public R UserPage(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false)UserVo userVo){
        Page<UcenterMember> page = new Page<>(current,limit);
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        String mobile = userVo.getMobile();
        String nickname = userVo.getNickname();
        if (!StringUtils.isEmpty(mobile)){
            queryWrapper.like("mobile",mobile);
        }
        if (!StringUtils.isEmpty(nickname)){
            queryWrapper.like("nickname",nickname);
        }
        ucenterMemberService.page(page,queryWrapper);
        long total = page.getTotal();
        List<UcenterMember> records = page.getRecords();
        return R.ok().data("total",total).data("records",records);
    }
    //id查询
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        UcenterMember member = ucenterMemberService.getById(id);
        return R.ok().data("member",member);
    }
    //修改
    @PostMapping("update")
    public R update(@RequestBody UcenterMember ucenterMember){
        ucenterMemberService.updateById(ucenterMember);
        return R.ok();
    }
    //删除
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id){
        ucenterMemberService.removeById(id);
        return R.ok();
    }

    @GetMapping("info")
    public R info(){


        return R.ok().data("roles","123456").data("name","15083747424").data("avatar","https://yang3413.oss-cn-beijing.aliyuncs.com/2020/05/11/26c7e5file.png");
    }
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo){


        return R.ok().data("entity","2345");
    }




}

